package summaryRanges;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        int len = nums.length;
        int start = nums[0];
        int index = 0;
        int i = 0;
        while(index<len){
            if(start + i!=nums[index]){
                StringBuilder sb = new StringBuilder();
                sb.append(start);
                if(start!=nums[index-1]){
                    sb.append("->").append(nums[index-1]);
                }
                i = 0;
                res.add(sb.toString());
                start = nums[index];
            }
            index++;
            i++;
        }
        if(start==nums[len-1]){
            res.add(Integer.toString(nums[len-1]));
        }else{
            res.add(Integer.toString(start)+"->"+Integer.toString(nums[len-1]));
        }
        return res;
    }
}